package com.softer.wxzj.service.impl;

import com.softer.wxzj.common.util.StringUtils;
import com.softer.wxzj.entity.FlowAuditdetail;
import com.softer.wxzj.entity.FlowInstance;
import com.softer.wxzj.mapper.FlowAuditdetailMapper;
import com.softer.wxzj.mapper.FlowInstanceMapper;
import com.softer.wxzj.service.FlowAuditdetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class FlowAuditdetailServiceImpl implements FlowAuditdetailService {
    @Autowired
    private FlowAuditdetailMapper flowAuditdetailMapper;
    @Autowired
    private FlowInstanceMapper flowInstanceMapper;

    @Override
    public List<FlowAuditdetail> getAuditdetailList(String businessId, String flowType) {
        List<FlowAuditdetail> list = flowAuditdetailMapper.getAuditdetailList(businessId, flowType);
        list.forEach(flowAuditdetail -> flowAuditdetail.setCreateTime(StringUtils.substring(flowAuditdetail.getCreateTime(),0,10)));
        return list;
    }
    @Override
    public List<FlowInstance> getHistoryList(String businessId, String flowState,String modifyId) {
        List<FlowInstance> list = flowInstanceMapper.getListByBusinessIdAndFlowType(businessId, flowState,modifyId);
        list.forEach(l ->{l.setCreateTime(StringUtils.substring(l.getCreateTime(),0,10));l.setEndTime(l.getEndTime());} );
        if(null != list){
            for (FlowInstance flowInstance:list) {
                List<FlowAuditdetail> detailList = flowAuditdetailMapper.getListByInstanceId(flowInstance.getId());
                detailList.forEach(l ->l.setCreateTime(StringUtils.substring(l.getCreateTime(),0,10)) );
                flowInstance.setList(detailList);
            }
        }
        return list;
    }
    @Override
    public List<FlowAuditdetail> getCurrentAuditdetailList(String businessId, String flowType) {
        return flowAuditdetailMapper.getCurrentAuditdetailList(businessId, flowType);
    }

}
